﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using JRCore.protocol;
using System.Threading;
using DataRecv.tools;
using System.Runtime.InteropServices;
using DataRecv.models.device;
using DataRecv.models;
using DataRecv.models.road;
using DataRecv.enums;
using System.IO;
using System.Collections;
using DataRecv.models.sdk.HKSDK;

namespace DataRecv.models.recever
{
    public class Recever_HK
    {
        public bool m_Init ;
        public bool m_CameraCallBackSet;
        public int m_HostCallBackSet;
        public Timer m_InitTimer;
        public CHCNetSDK.MSGCallBack m_CameraCallBack;
        public CHCNetSDK.MSGCallBack m_HostCallBack;

        private static Hashtable m_Records;
        private static object lockA = false;
        private static object lockB = false;
        public Recever_HK()
        {
            m_HostCallBackSet = -1;
            m_InitTimer = new Timer(new TimerCallback(InitTimerCallbackFunc), null, 1000, 60000);
            //m_secretkey = SecretKey.GetKey(G.SystemParam.LocalIP);
            m_Records = new Hashtable();
        }

        /// <summary>
        /// 调用相应的设备SDK开始接收
        /// </summary>
        private void InitTimerCallbackFunc(object obj)
        {
            try
            {
                if (m_Init == false)
                {
                    m_Init = CHCNetSDK.NET_DVR_Init();//初始化SDK
                    CHCNetSDK.NET_DVR_SetConnectTime(2000, 1);    //2.设置连接超时时间ms
                    CHCNetSDK.NET_DVR_SetReconnect(10000, 1);  //3.设置重连间隔ms
                }

                if (m_Init==true)
                {
                    if (m_CameraCallBackSet == false)
                    {
                        m_CameraCallBack = new CHCNetSDK.MSGCallBack(HKCameraFunc);
                        m_CameraCallBackSet = CHCNetSDK.NET_DVR_SetDVRMessageCallBack_V30(m_CameraCallBack, IntPtr.Zero);
                        LogTool.log.Info("HK布防回调设置" + (m_CameraCallBackSet ? "成功" : "失败"));
                    }

                    if (m_HostCallBackSet < 0)
                    {
                        m_HostCallBack = new CHCNetSDK.MSGCallBack(HKHostFunc);
                        m_HostCallBackSet = CHCNetSDK.NET_DVR_StartListen_V30(null, 5650, m_HostCallBack, IntPtr.Zero);
                        LogTool.log.Info("HK监听回调设置" + ((m_HostCallBackSet < 0) ? ("失败code=" + CHCNetSDK.NET_DVR_GetLastError().ToString()) : "成功"));
                    }
                }
            }
            catch(Exception er)
            {
                LogTool.log.Info("Recever_HK.InitTimerCallbackFunc()" + er.Message);
            }
        }

        //“海康主机”回调函数
        private void HKHostFunc(int lCommand, ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
        {
            try
            {
                lock (lockB)
                {
                    if (lCommand == 0x3050)
                    {
                        G.N++;
                        Recever_Bag bag = new Recever_Bag();
                        bag.ip = pAlarmer.sDeviceIP;
                        bag.id = G.N;
                        bag.type = "HK5012";
                        if (LogTool.gDebugEnable) LogTool.log.Info("1H " + bag.Txt());
                        bag.NET_ITS_PLATE_RESULT = (CHCNetSDK.NET_ITS_PLATE_RESULT)Marshal.PtrToStructure(pAlarmInfo, typeof(CHCNetSDK.NET_ITS_PLATE_RESULT));
                        //ThreadPool.QueueUserWorkItem(new WaitCallback(HKHostThreadFunc), bag);
                        HKHostThreadFunc(bag);
                    }
                }
            }
            catch (Exception er)
            {
                LogTool.log.Info("HKHostFunc() N=" + G.N.ToString() + " HK5012 ip=" + pAlarmer.sDeviceIP + er.Message);
                if (er.Message.Contains("OutOfMemoryException"))
                {
                    GC.Collect();
                }
            }
        }

        //“海康摄像机”回调函数
        private void HKCameraFunc(int lCommand, ref CHCNetSDK.NET_DVR_ALARMER pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
        {
            try
            {
                lock (lockA)
                {
					//新报警信息类型
                    if (lCommand == 0x3050)
                    {
                        G.N++;
                        Recever_Bag bag = new Recever_Bag();
                        bag.ip = pAlarmer.sDeviceIP;
                        bag.id = G.N;
                        bag.type = "HKA";
                        if (LogTool.gDebugEnable) LogTool.log.Info("1A " + bag.Txt());
                        bag.NET_ITS_PLATE_RESULT = (CHCNetSDK.NET_ITS_PLATE_RESULT)Marshal.PtrToStructure(pAlarmInfo, typeof(CHCNetSDK.NET_ITS_PLATE_RESULT));
                        //ThreadPool.QueueUserWorkItem(new WaitCallback(HKCameraAThreadFunc), bag);
                        HKCameraAThreadFunc(bag);
                    }

                    //老报警信息类型
                    if (lCommand == 0x2800)
                    {
                        G.N++;
                        Recever_Bag bag = new Recever_Bag();
                        bag.ip = pAlarmer.sDeviceIP;
                        bag.id = G.N;
                        bag.type = "HKB";
                        if (LogTool.gDebugEnable) LogTool.log.Info("1B " + bag.Txt());
                        bag.NET_DVR_PLATE_RESULT = (CHCNetSDK.NET_DVR_PLATE_RESULT)Marshal.PtrToStructure(pAlarmInfo, typeof(CHCNetSDK.NET_DVR_PLATE_RESULT));
                        //ThreadPool.QueueUserWorkItem(new WaitCallback(HKCameraBThreadFunc), bag);
                        HKCameraBThreadFunc(bag);
                    }
                }
            }
            catch (Exception er)
            {
                LogTool.log.Info("HKCameraFunc() N=" + G.N.ToString() + " HKcam ip=" + pAlarmer.sDeviceIP + er.Message);
                if (er.Message.Contains("OutOfMemoryException"))
                {
                    GC.Collect();
                }
                
            }
        }

        private void HKHostThreadFunc(object obj)
        {
            Recever_Bag bag = (Recever_Bag)obj;
            
            try
            {
                CHCNetSDK.NET_ITS_PLATE_RESULT Result = bag.NET_ITS_PLATE_RESULT;

                #region 1数据转换
                string P4 = "0";//判断是否是合成图

                for (int j = 0; j < Result.dwPicNum; j++)
                {
                    if (Result.struPicInfo[j].byType == 2)
                    {
                        P4 = "1";
                        break;
                    }
                }

                //海康主机byIllegalType始终=0，在此修正,如果图片数量大于2或者是合成图，就认为是违法图片
                if ((Result.dwPicNum > 2) || (P4 == "1"))
                {
                    switch (Result.wIllegalType)
                    {
                        case 1349://机动车行驶超过规定时速50%以下
                            Result.struVehicleInfo.byIllegalType = 1;//低速;
                            break;
                        case 1632://机动车行驶超过规定时速50%
                            Result.struVehicleInfo.byIllegalType = 2;//超速;
                            break;
                        case 1301:
                            Result.struVehicleInfo.byIllegalType = 3;//逆行;
                            break;
                        case 1302:
                        case 1625:
                            Result.struVehicleInfo.byIllegalType = 4;//闯红灯;
                            break;
                        case 13451:
                            Result.struVehicleInfo.byIllegalType = 5;//压线;
                            break;
                        case 1208:
                            Result.struVehicleInfo.byIllegalType = 6;//不按导线行驶;
                            break;
                        case 1344:
                            Result.struVehicleInfo.byIllegalType = 9;//违章变道;
                            break;
                        case 1211:
                        case 4613:
                            Result.struVehicleInfo.byIllegalType = 12;//路口停车,违法停车;
                            break;
                        default:
                            break;
                    }
                }

                string P7 = "2";//采集方式(必须)1:闯红灯自动记录设备2:公路车辆智能监控设备3:测速设备4:闭路电视5:移动摄像6:数码相机7:区间测速8:卫星定位装置9:其它电子设备11:群众举报
                switch (Result.struVehicleInfo.byIllegalType)
                {
                    case 0: P7 = "2";
                        break;
                    case 1: P7 = "3";
                        break;
                    case 2: P7 = "3";
                        break;
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                    case 10:
                    case 11:
                    case 12:
                        P7 = "1";
                        break;
                    default:
                        break;
                }
                bag.bz = Result.struVehicleInfo.byIllegalType.ToString();
                if (LogTool.gDebugEnable) LogTool.log.Info("2H " + bag.Txt());
                DeviceView thisview = (DeviceView)G.Views[bag.ip];

                string P16 = "";
                switch (Result.byDir)
                {
                    case 4: P16 = "1";
                        break;
                    case 5: P16 = "2";
                        break;
                    case 6: P16 = "3";
                        break;
                    case 7: P16 = "4";
                        break;
                    default: P16 = "1";
                        break;
                }

                string P18 = Encoding.Default.GetString(Result.struPicInfo[0].byAbsTime).Trim('\0');
                string yyyy = P18.Substring(0, 4);
                string MM = P18.Substring(4, 2);
                string dd = P18.Substring(6, 2);
                string HH = P18.Substring(8, 2);
                string mm = P18.Substring(10, 2);
                string ss = P18.Substring(12, 2);
                string fff = P18.Substring(14, 3);
                string P19 = yyyy + "-" + MM + "-" + dd + " " + HH + ":" + mm + ":" + ss;


                string plateWithColor = Result.struPlateInfo.sLicense.ToString();
                string P21 = "";
                string P22 = "";
                string P23 = "";

                if (plateWithColor.Contains("蓝"))
                {
                    P22 = "蓝";
                    P23 = plateWithColor.Replace("蓝", "");
                }
                else if (plateWithColor.Contains("黄"))
                {
                    P22 = "黄";
                    P23 = plateWithColor.Replace("黄", "");
                }
                else if (plateWithColor.Contains("白"))
                {
                    P22 = "白";
                    P23 = plateWithColor.Replace("白", "");
                }
                else if (plateWithColor.Contains("黑"))
                {
                    P22 = "黑";
                    P23 = plateWithColor.Replace("黑", "");
                }
                else
                {
                    P22 = "";
                    P23 = plateWithColor;
                }

                if (P23.Contains("挂"))
                {
                    P21 = "15";
                    P23 = P23.Replace("挂", "");
                }
                else if (P23.Contains("学"))
                {
                    P21 = "16";
                    P23 = P23.Replace("学", "");
                }
                else if (P23.Contains("警"))
                {
                    P21 = "23";
                    P23 = P23.Replace("警", "");
                }
                else if (P22.Contains("蓝"))
                {
                    P21 = "02";
                }
                else if (P22.Contains("黄"))
                {
                    P21 = "01";
                }
                else
                {
                    P21 = "";
                }

                //车辆类型：0- 未知，1- 客车，2- 货车，3- 轿车，4- 面包车，5- 小货车
                //0- 未知，1- 客车(大型)，2- 货车(大型)，3- 轿车(小型)，4- 面包车，5- 小货车，6- 行人，7- 二轮车，8- 三轮车，9- SUV/MPV，10- 中型客车 
                string P44 = Result.byVehicleType.ToString();
                
                    #endregion

                #region 2信息赋值
                DataRecvRecord record = new DataRecvRecord();
                record.P1 = "3.1";
                record.P2 = Result.struVehicleInfo.dwIndex.ToString();
                record.P3 = "3";
                record.P4 = P4;
                record.P5 = Result.struVehicleInfo.byIllegalType.ToString();
                record.P6 = "JARI";
                record.P7 = P7;
                record.P8 = thisview.deviceparam.sbbh;
                record.P9 = "SJZD" + G.SystemParam.LocalIP;
                record.P10 = "";
                record.P11 = thisview.roadparam.gbdm;
                record.P12 = thisview.roadparam.bmbh;
                record.P13 = thisview.roadparam.xzqh;
                record.P14 = thisview.roadparam.roadid;
                record.P15 = thisview.roadparam.roadname;
                record.P16 = P16;//((int)thisview.deviceparam.direction).ToString();//HOST方向不能由配置获取
                record.P17 = Result.byDriveChan.ToString();
                record.P18 = P18;
                record.P19 = P19;
                record.P20 = "0";
                record.P21 = P21;
                record.P22 = P22;
                record.P23 = P23;
                record.P24 = Result.struVehicleInfo.byColor.ToString();
                record.P25 = Result.struVehicleInfo.wLength.ToString();
                record.P26 = thisview.deviceparam.speedlimit;
                record.P27 = Result.struVehicleInfo.wSpeed.ToString();
                record.P28 = "0";
                record.P29 = G.SystemParam.LocalIP;
                record.P30 = "";
                record.P31 = "";
                record.P32 = "";
                record.P33 = "";
                record.P34 = "";
                record.P35 = "";
                record.P36 = "";
                record.P37 = "";
                record.P38 = "";
                record.P39 = "";
                record.P40 = "";
                record.P41 = "";
                record.P42 = "";
                record.P43 = "";
                record.P44 = P44;
                record.P45 = "";
                record.P46 = "";
                record.P47 = "";
                record.P48 = "";
                record.P49 = "";
                record.P50 = "";
                record.P51 = "";
                record.P52 = "";
                record.P53 = "";
                #endregion

                #region 3图片赋值
                for (int i = 0; i < Result.dwPicNum; i++)
                {
                    switch (Result.struPicInfo[i].byType)
                    {
                        case 0://车牌图
                            record.Pic5 = new byte[Result.struPicInfo[i].dwDataLen];
                            Marshal.Copy(Result.struPicInfo[i].pBuffer, record.Pic5, 0, (int)Result.struPicInfo[i].dwDataLen);
                            break;
                        case 1://场景图
                            if (record.Pic1 == null)
                            {
                                record.P3 = "1";
                                record.Pic1 = new byte[(int)Result.struPicInfo[i].dwDataLen];
                                Marshal.Copy(Result.struPicInfo[i].pBuffer, record.Pic1, 0, record.Pic1.Length);
                            }
                            else if (record.Pic2 == null)
                            {
                                record.P3 = "2";
                                record.Pic2 = new byte[(int)Result.struPicInfo[i].dwDataLen];
                                Marshal.Copy(Result.struPicInfo[i].pBuffer, record.Pic2, 0, record.Pic2.Length);
                            }
                            else if (record.Pic3 == null)
                            {
                                record.P3 = "3";
                                record.Pic3 = new byte[(int)Result.struPicInfo[i].dwDataLen];
                                Marshal.Copy(Result.struPicInfo[i].pBuffer, record.Pic3, 0, record.Pic3.Length);
                            }
                            else if (record.Pic4 == null)
                            {
                                record.P3 = "4";
                                record.Pic4 = new byte[(int)Result.struPicInfo[i].dwDataLen];
                                Marshal.Copy(Result.struPicInfo[i].pBuffer, record.Pic4, 0, record.Pic4.Length);
                            }
                            break;
                        case 2://合成图
                            record.P3 = "3";//合成图默认抓3张
                            record.Pic1 = new byte[(int)Result.struPicInfo[i].dwDataLen];
                            Marshal.Copy(Result.struPicInfo[i].pBuffer, record.Pic1, 0, record.Pic1.Length);
                            break;
                        case 3://特写图
                            record.Pic6 = new byte[(int)Result.struPicInfo[i].dwDataLen];
                            Marshal.Copy(Result.struPicInfo[i].pBuffer, record.Pic6, 0, record.Pic6.Length);//图片不能放在别的线程
                            break;
                        default:
                            break;
                    }
                }
                #endregion


                Worker_Bag workbag = new Worker_Bag();
                workbag.record = record;
                workbag.view = thisview;
                //调试用参数
                workbag.ip = bag.ip;
                workbag.id = bag.id;
                workbag.type = bag.type;
                workbag.bz = bag.bz;
                if (G.Worker != null) G.Worker.AddJob(workbag);
            }
            catch (Exception er)
            {
                LogTool.log.Info("HKHostThreadFunc() " + bag.Txt() + er.Message);
                if (er.Message.Contains("OutOfMemoryException"))
                {
                    GC.Collect();
                }
                
            }
        }

        private void HKCameraAThreadFunc(object obj)
        {
            Recever_Bag bag = (Recever_Bag)obj;
            
            try
            {
                
                CHCNetSDK.NET_ITS_PLATE_RESULT Result = bag.NET_ITS_PLATE_RESULT;

                #region 1数据转换
                string P4 = "0";//判断是否是合成图

                for (int j = 0; j < Result.dwPicNum; j++)
                {
                    if (Result.struPicInfo[j].byType == 2)
                    {
                        P4 = "1";
                        break;
                    }
                }

                
                switch (Result.wIllegalType)
                {
                    case 1349://机动车行驶超过规定时速50%以下
                        Result.struVehicleInfo.byIllegalType = 1;//低速;
                        break;
                    case 1632://机动车行驶超过规定时速50%
                        Result.struVehicleInfo.byIllegalType = 2;//超速;
                        break;
                    case 1301:
                        Result.struVehicleInfo.byIllegalType = 3;//逆行;
                        break;
                    case 1302:
                    case 1625:
                        Result.struVehicleInfo.byIllegalType = 4;//闯红灯;
                        break;
                    case 13451:
                        Result.struVehicleInfo.byIllegalType = 5;//压线;
                        break;
                    case 1208:
                        Result.struVehicleInfo.byIllegalType = 6;//不按导线行驶;
                        break;
                    case 1344:
                        Result.struVehicleInfo.byIllegalType = 9;//违章变道;
                        break;
                    case 1211:
                    case 4613:
                        Result.struVehicleInfo.byIllegalType = 12;//路口停车,违法停车;
                        break;
                    default:
                        break;
                }
                LogTool.log.Info("wIllegalType=" + Result.wIllegalType + " byIllegalType=" + Result.struVehicleInfo.byIllegalType);

                string P7 = "2";//采集方式(必须)1:闯红灯自动记录设备2:公路车辆智能监控设备3:测速设备4:闭路电视5:移动摄像6:数码相机7:区间测速8:卫星定位装置9:其它电子设备11:群众举报
                switch (Result.struVehicleInfo.byIllegalType)
                {
                    case 0: P7 = "2";
                        break;
                    case 1: P7 = "3";
                        break;
                    case 2: P7 = "3";
                        break;
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                    case 10:
                    case 11:
                    case 12:
                        P7 = "1";
                        break;
                    default:
                        break;
                }
                bag.bz = Result.struVehicleInfo.byIllegalType.ToString();
                if (LogTool.gDebugEnable) LogTool.log.Info("2A " + bag.Txt());
                DeviceView thisview = (DeviceView)G.Views[bag.ip];

                string P18 = Encoding.Default.GetString(Result.struPicInfo[0].byAbsTime).Trim('\0');
                string yyyy = P18.Substring(0, 4);
                string MM = P18.Substring(4, 2);
                string dd = P18.Substring(6, 2);
                string HH = P18.Substring(8, 2);
                string mm = P18.Substring(10, 2);
                string ss = P18.Substring(12, 2);
                string fff = P18.Substring(14, 3);
                string P19 = yyyy + "-" + MM + "-" + dd + " " + HH + ":" + mm + ":" + ss;


                string plateWithColor = Result.struPlateInfo.sLicense.ToString();
                string P21 = "";
                string P22 = "";
                string P23 = "";

                if (plateWithColor.Contains("蓝"))
                {
                    P22 = "蓝";
                    P23 = plateWithColor.Replace("蓝", "");
                }
                else if (plateWithColor.Contains("黄"))
                {
                    P22 = "黄";
                    P23 = plateWithColor.Replace("黄", "");
                }
                else if (plateWithColor.Contains("白"))
                {
                    P22 = "白";
                    P23 = plateWithColor.Replace("白", "");
                }
                else if (plateWithColor.Contains("黑"))
                {
                    P22 = "黑";
                    P23 = plateWithColor.Replace("黑", "");
                }
                else if (plateWithColor.Contains("无"))
                {
                    P22 = "";
                    P23 = "";
                }
                else
                {
                    P22 = "";
                    P23 = plateWithColor;
                }

                if (P23.Contains("挂"))
                {
                    P21 = "15";
                    P23 = P23.Replace("挂", "");
                }
                else if (P23.Contains("学"))
                {
                    P21 = "16";
                    P23 = P23.Replace("学", "");
                }
                else if (P23.Contains("警"))
                {
                    P21 = "23";
                    P23 = P23.Replace("警", "");
                }
                else if (P22.Contains("蓝"))
                {
                    P21 = "02";
                }
                else if (P22.Contains("黄"))
                {
                    P21 = "01";
                }
                else
                {
                    P21 = "";
                }


                //车辆类型：0- 未知，1- 客车，2- 货车，3- 轿车，4- 面包车，5- 小货车
                //0- 未知，1- 客车(大型)，2- 货车(大型)，3- 轿车(小型)，4- 面包车，5- 小货车，6- 行人，7- 二轮车，8- 三轮车，9- SUV/MPV，10- 中型客车 
                string P44 = Result.byVehicleType.ToString();
                

                #endregion

                #region 2信息赋值
                DataRecvRecord record = new DataRecvRecord();
                record.P1 = "3.1";
                record.P2 = Result.struVehicleInfo.dwIndex.ToString();
                record.P3 = "3";
                record.P4 = P4;
                record.P5 = Result.struVehicleInfo.byIllegalType.ToString();
                record.P6 = "JARI";
                record.P7 = P7;
                record.P8 = thisview.deviceparam.sbbh;
                record.P9 = "SJZD" + G.SystemParam.LocalIP;
                record.P10 = "";
                record.P11 = thisview.roadparam.gbdm;
                record.P12 = thisview.roadparam.bmbh;
                record.P13 = thisview.roadparam.xzqh;
                record.P14 = thisview.roadparam.roadid;
                record.P15 = thisview.roadparam.roadname;
                record.P16 = ((int)thisview.deviceparam.direction).ToString();
                record.P17 = Result.byDriveChan.ToString();
                record.P18 = P18;
                record.P19 = P19;
                record.P20 = "0";
                record.P21 = P21;
                record.P22 = P22;
                record.P23 = P23;
                record.P24 = Result.struVehicleInfo.byColor.ToString();
                record.P25 = Result.struVehicleInfo.wLength.ToString();
                record.P26 = thisview.deviceparam.speedlimit;
                record.P27 = Result.struVehicleInfo.wSpeed.ToString();
                record.P28 = "0";
                record.P29 = G.SystemParam.LocalIP;
                record.P30 = "";
                record.P31 = "";
                record.P32 = "";
                record.P33 = "";
                record.P34 = "";
                record.P35 = "";
                record.P36 = "";
                record.P37 = "";
                record.P38 = "";
                record.P39 = "";
                record.P40 = "";
                record.P41 = "";
                record.P42 = "";
                record.P43 = "";
                record.P44 = P44;
                record.P45 = "";
                record.P46 = "";
                record.P47 = "";
                record.P48 = "";
                record.P49 = "";
                record.P50 = "";
                record.P51 = "";
                record.P52 = "";
                record.P53 = "";
                #endregion

                #region 3图片赋值
                for (int i = 0; i < Result.dwPicNum; i++)
                {
                    switch (Result.struPicInfo[i].byType)
                    {
                        case 0://车牌图
                            record.Pic5 = new byte[Result.struPicInfo[i].dwDataLen];
                            Marshal.Copy(Result.struPicInfo[i].pBuffer, record.Pic5, 0, (int)Result.struPicInfo[i].dwDataLen);
                            break;
                        case 1://场景图
                            if (record.Pic1 == null)
                            {
                                record.P3 = "1";
                                record.Pic1 = new byte[(int)Result.struPicInfo[i].dwDataLen];
                                Marshal.Copy(Result.struPicInfo[i].pBuffer, record.Pic1, 0, record.Pic1.Length);
                            }
                            else if (record.Pic2 == null)
                            {
                                record.P3 = "2";
                                record.Pic2 = new byte[(int)Result.struPicInfo[i].dwDataLen];
                                Marshal.Copy(Result.struPicInfo[i].pBuffer, record.Pic2, 0, record.Pic2.Length);
                            }
                            else if (record.Pic3 == null)
                            {
                                record.P3 = "3";
                                record.Pic3 = new byte[(int)Result.struPicInfo[i].dwDataLen];
                                Marshal.Copy(Result.struPicInfo[i].pBuffer, record.Pic3, 0, record.Pic3.Length);
                            }
                            else if (record.Pic4 == null)
                            {
                                record.P3 = "4";
                                record.Pic4 = new byte[(int)Result.struPicInfo[i].dwDataLen];
                                Marshal.Copy(Result.struPicInfo[i].pBuffer, record.Pic4, 0, record.Pic4.Length);
                            }
                            break;
                        case 2://合成图
                            record.P3 = "3";//合成图默认抓3张
                            record.Pic1 = new byte[(int)Result.struPicInfo[i].dwDataLen];
                            Marshal.Copy(Result.struPicInfo[i].pBuffer, record.Pic1, 0, record.Pic1.Length);
                            break;
                        case 3://特写图
                            record.Pic6 = new byte[(int)Result.struPicInfo[i].dwDataLen];
                            Marshal.Copy(Result.struPicInfo[i].pBuffer, record.Pic6, 0, record.Pic6.Length);//图片不能放在别的线程
                            break;
                        default:
                            break;
                    }
                }
                #endregion

                Worker_Bag workbag = new Worker_Bag();
                workbag.record = record;
                workbag.view = thisview;
                //调试用参数
                workbag.ip = bag.ip;
                workbag.id = bag.id;
                workbag.type = bag.type;
                workbag.bz = bag.bz;
                if (G.Worker != null) G.Worker.AddJob(workbag);
            }
            catch (Exception er)
            {
                LogTool.log.Info("HKCameraAThreadFunc() " + bag.Txt() + er.Message);
                if (er.Message.Contains("OutOfMemoryException"))
                {
                    GC.Collect();
                }
            }
        }

        private void HKCameraBThreadFunc(object obj)
        {
            Recever_Bag bag = (Recever_Bag)obj;
            
            try
            {
                
                CHCNetSDK.NET_DVR_PLATE_RESULT Result = bag.NET_DVR_PLATE_RESULT;

                #region 1数据转换

                //德兴超速第一张Result.byPicNum=1,第二张Result.byPicNum=1
                //张家界超速第一张Result.byPicNum=1,第二张Result.byPicNum=2
                if (Result.struVehicleInfo.byIllegalType == 2)
                { 
                    if(m_Records.Contains(bag.ip))
                    {
                        if (Result.byPicNum == 1)
                        {
                            Result.byPicNum = 2;
                        }
                    }
                }

                DataRecvRecord record = null;
                if (m_Records.Contains(bag.ip) && (Result.byPicNum>1))//电警的第2，3张才取出以前的单张记录
                {
                    record = (DataRecvRecord)m_Records[bag.ip];
                }
                else
                {
                    record = new DataRecvRecord();
                }

                string P4 = "0";//判断是否是合成图
                string P7 = "2";//采集方式(必须)1:闯红灯自动记录设备2:公路车辆智能监控设备3:测速设备4:闭路电视5:移动摄像6:数码相机7:区间测速8:卫星定位装置9:其它电子设备11:群众举报
                switch (Result.struVehicleInfo.byIllegalType)
                {
                    case 0: P7 = "2";
                        break;
                    case 1: P7 = "3";
                        break;
                    case 2: P7 = "3";
                        break;
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                    case 10:
                    case 11: P7 = "1";
                        break;
                    case 15:
                        P7 = "4";
                        break;
                    default:
                        break;
                }

                //确定记录类型
                Enum_CaptureSystem cs = Enum_CaptureSystem.NONE;
                switch (Result.struVehicleInfo.byIllegalType)
                {
                    case 0: cs = Enum_CaptureSystem.卡口;
                        break;
                    case 1: cs = Enum_CaptureSystem.低速;
                        break;
                    case 2: cs = Enum_CaptureSystem.超速;
                        break;
                    case 3: cs = Enum_CaptureSystem.逆行;
                        break;
                    case 4: cs = Enum_CaptureSystem.闯红灯;
                        break;
                    case 5: cs = Enum_CaptureSystem.压线;
                        break;
                    case 6: cs = Enum_CaptureSystem.不按导向行驶;
                        break;
                    default: cs = Enum_CaptureSystem.卡口;
                        break;
                }
                bag.bz = Result.struVehicleInfo.byIllegalType.ToString();
                if (LogTool.gDebugEnable) LogTool.log.Info("2B " + bag.Txt());

                SnapParam sp = (SnapParam)G.SystemParam.SnapParams[cs];
                DeviceView thisview = (DeviceView)G.Views[bag.ip];

                string P18 = Encoding.Default.GetString(Result.byAbsTime).Trim('\0');
                string yyyy = P18.Substring(0, 4);
                string MM = P18.Substring(4, 2);
                string dd = P18.Substring(6, 2);
                string HH = P18.Substring(8, 2);
                string mm = P18.Substring(10, 2);
                string ss = P18.Substring(12, 2);
                string fff = P18.Substring(14, 3);
                string P19 = yyyy + "-" + MM + "-" + dd + " " + HH + ":" + mm + ":" + ss;


                string plateWithColor = Result.struPlateInfo.sLicense.ToString();
                string P21 = "";
                string P22 = "";
                string P23 = "";

                if (plateWithColor.Contains("蓝"))
                {
                    P22 = "蓝";
                    P23 = plateWithColor.Replace("蓝", "");
                }
                else if (plateWithColor.Contains("黄"))
                {
                    P22 = "黄";
                    P23 = plateWithColor.Replace("黄", "");
                }
                else if (plateWithColor.Contains("白"))
                {
                    P22 = "白";
                    P23 = plateWithColor.Replace("白", "");
                }
                else if (plateWithColor.Contains("黑"))
                {
                    P22 = "黑";
                    P23 = plateWithColor.Replace("黑", "");
                }
                else if (plateWithColor.Contains("无"))
                {
                    P22 = "";
                    P23 = "";
                }
                else
                {
                    P22 = "";
                    P23 = plateWithColor;
                }

                if (P23.Contains("挂"))
                {
                    P21 = "15";
                    P23 = P23.Replace("挂", "");
                }
                else if (P23.Contains("学"))
                {
                    P21 = "16";
                    P23 = P23.Replace("学", "");
                }
                else if (P23.Contains("警"))
                {
                    P21 = "23";
                    P23 = P23.Replace("警", "");
                }
                else if (P22.Contains("蓝"))
                {
                    P21 = "02";
                }
                else if (P22.Contains("黄"))
                {
                    P21 = "01";
                }
                else
                {
                    P21 = "";
                }


                //车辆类型：0- 未知，1- 客车，2- 货车，3- 轿车，4- 面包车，5- 小货车
                //0- 未知，1- 客车(大型)，2- 货车(大型)，3- 轿车(小型)，4- 面包车，5- 小货车，6- 行人，7- 二轮车，8- 三轮车，9- SUV/MPV，10- 中型客车 
                string P44 = Result.struVehicleInfo.byVehicleType.ToString();
                
                #endregion

                #region 2信息赋值

                if (Result.byPicNum == 0)
                {
                    record.P1 = "3.1";
                    record.P2 = Result.struVehicleInfo.dwIndex.ToString();
                    record.P3 = sp.snapcount.ToString();
                    record.P4 = P4;
                    record.P5 = Result.struVehicleInfo.byIllegalType.ToString();
                    record.P6 = "JARI";
                    record.P7 = P7;
                    record.P8 = thisview.deviceparam.sbbh;
                    record.P9 = "SJZD" + G.SystemParam.LocalIP;
                    record.P10 = "";
                    record.P11 = thisview.roadparam.gbdm;
                    record.P12 = thisview.roadparam.bmbh;
                    record.P13 = thisview.roadparam.xzqh;
                    record.P14 = thisview.roadparam.roadid;
                    record.P15 = thisview.roadparam.roadname;
                    record.P16 = ((int)thisview.deviceparam.direction).ToString();
                    record.P17 = Result.byDriveChan.ToString();
                    record.P18 = P18;
                    record.P19 = P19;
                    record.P20 = "0";
                    record.P21 = P21;
                    record.P22 = P22;
                    record.P23 = P23;
                    record.P24 = Result.struVehicleInfo.byColor.ToString();
                    record.P25 = Result.struVehicleInfo.wLength.ToString();
                    record.P26 = thisview.deviceparam.speedlimit;
                    record.P27 = Result.struVehicleInfo.wSpeed.ToString();
                    record.P28 = "0";
                    record.P29 = G.SystemParam.LocalIP;
                    record.P44 = P44;

                    //车牌图
                    int platesize = (int)Result.dwPicPlateLen;
                    if ((platesize > 0) && (P23 != ""))
                    {
                        record.Pic5 = new byte[platesize];
                        Marshal.Copy(Result.pBuffer2, record.Pic5, 0, platesize);
                    }
                    else
                    {
                        record.Pic5 = null;
                    }
                    Result.byPicNum = 1;//卡口连拍序号byPicNum=0，不等于sp.snapcount=1，修正
                }
                else
                {
                    if (sp.timefrom == Result.byPicNum)
                    {
                        record.P18 = P18;
                        record.P19 = P19;
                    }
                    if (sp.platefrom == Result.byPicNum)
                    {
                        record.P21 = P21;
                        record.P22 = P22;
                        record.P23 = P23;
                        record.P44 = P44;

                        //车牌图
                        int platesize = (int)Result.dwPicPlateLen;
                        if ((platesize > 0) && (P23 != ""))
                        {
                            record.Pic5 = new byte[platesize];
                            Marshal.Copy(Result.pBuffer2, record.Pic5, 0, platesize);
                        }
                        else
                        {
                            record.Pic5 = null;
                        }
                    }
                    if (sp.speedfrom == Result.byPicNum)
                    {
                        record.P26 = thisview.deviceparam.speedlimit;
                        record.P27 = Result.struVehicleInfo.wSpeed.ToString();
                    }
                    if (sp.otherfrom == Result.byPicNum)
                    {
                        record.P1 = "3.1";
                        record.P2 = Result.struVehicleInfo.dwIndex.ToString();
                        record.P3 = sp.snapcount.ToString();
                        record.P4 = P4;
                        record.P5 = Result.struVehicleInfo.byIllegalType.ToString();
                        record.P6 = "JARI";
                        record.P7 = P7;
                        record.P8 = thisview.deviceparam.sbbh;
                        record.P9 = "SJZD" + G.SystemParam.LocalIP;
                        record.P10 = "";
                        record.P11 = thisview.roadparam.gbdm;
                        record.P12 = thisview.roadparam.bmbh;
                        record.P13 = thisview.roadparam.xzqh;
                        record.P14 = thisview.roadparam.roadid;
                        record.P15 = thisview.roadparam.roadname;
                        record.P16 = ((int)thisview.deviceparam.direction).ToString();
                        record.P17 = Result.byDriveChan.ToString();
                        record.P20 = "0";
                        record.P24 = Result.struVehicleInfo.byColor.ToString();
                        record.P25 = Result.struVehicleInfo.wLength.ToString();
                        record.P28 = "0";
                        record.P29 = G.SystemParam.LocalIP;
                    }
                }
                //场景图
                int picsize = (int)Result.dwPicLen;
                if (picsize > 0)
                {
                    if (Result.byPicNum == 0)
                    {
                        record.Pic1 = new byte[picsize];
                        Marshal.Copy(Result.pBuffer1, record.Pic1, 0, picsize);
                    }
                    else if (Result.byPicNum == 1)
                    {
                        record.Pic1 = new byte[picsize];
                        Marshal.Copy(Result.pBuffer1, record.Pic1, 0, picsize);
                    }
                    else if (Result.byPicNum == 2)
                    {
                        record.Pic2 = new byte[picsize];
                        Marshal.Copy(Result.pBuffer1, record.Pic2, 0, picsize);
                    }
                    else if (Result.byPicNum == 3)
                    {
                        record.Pic3 = new byte[picsize];
                        Marshal.Copy(Result.pBuffer1, record.Pic3, 0, picsize);
                    }
                    else if (Result.byPicNum == 4)
                    {
                        record.Pic4 = new byte[picsize];
                        Marshal.Copy(Result.pBuffer1, record.Pic4, 0, picsize);
                    }
                }
                #endregion

                #region 3提交处理
                if (LogTool.gDebugEnable) LogTool.log.Info("Result.byPicNum=" + Result.byPicNum.ToString() + " sp.snapcount=" + sp.snapcount.ToString());
                if (Result.byPicNum < sp.snapcount)
                {
                    m_Records[bag.ip] = record;
                }
                else if (Result.byPicNum == sp.snapcount)
                { 
                    Worker_Bag workbag = new Worker_Bag();
                    workbag.record = record;
                    workbag.view = thisview;
                    //调试用参数
                    workbag.ip = bag.ip;
                    workbag.id = bag.id;
                    workbag.type = bag.type;
                    workbag.bz = bag.bz;
                    if (G.Worker != null) G.Worker.AddJob(workbag);
                    m_Records.Remove(bag.ip);
                }
                #endregion

            }
            catch (Exception er)
            {
                LogTool.log.Info("HKCameraBThreadFunc()" + bag.Txt() + er.Message);
                if (er.Message.Contains("OutOfMemoryException"))
                {
                    GC.Collect();
                }
            }
        }

        /// <summary>
        /// 释放海康SDK资源
        /// </summary>
        public void Close()
        {
            try
            {
                if (m_Records != null)
                {
                    m_Records.Clear();
                }
            }
            catch { }

            try
            {
                if (m_InitTimer != null) m_InitTimer.Dispose();
            }
            catch { }

            try
            {
                CHCNetSDK.NET_DVR_Cleanup();
            }
            catch { }

            GC.Collect();
        }

        //public abstract void Run();
        //public abstract bool Save(DataRecvRecord record);
        //public abstract bool Upload(DataRecvRecord record);
    }
}
